<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" :align-center="true" width="50%" :title="`用户详情`">
    <el-form
      ref="integralAddFormRef"
      :rules="rules"
      :disabled="dialogData.isView"
      :model="dialogData.itemInfo"
      label-width="100px"
      label-suffix=" :"
      :hide-required-asterisk="dialogData.isView"
    >
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" v-show="!dialogData.isView" @click="handleSubmit" :loading="btnLoading">确定</el-button>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { getOptTitle, OptEnum } from "@/biz/eenum/appEnum";
import { DialogParam } from "@/biz/interface/DialogType";
import { IntegralAddEntity } from "@/biz/interface/integralType";
import { getIntegralAddInfoApi, saveIntegralAddApi } from "@/api/modules/sysitg/adt";
//页面的数据
interface IntegralAddDialogOption extends DialogParam {
  //弹出表单的标题
  title: string;
  //是否是查看状态，查看状态是不可以提交数据
  isView: boolean;
  //是否是审核状态
  isAudit: boolean;
  //当前的数据对象
  itemInfo: IntegralAddEntity;
  //其它的数据都继续在这里加
}
//提交按钮，避免多次提交
const btnLoading = ref(false);
//弹出框的参数
const dialogVisible = ref(false);
//当前表单数据在这里初始化
const dialogData = reactive<IntegralAddDialogOption>({
  optType: OptEnum.UNKNOW,
  title: "",
  isView: false,
  isAudit: false,
  itemInfo: {
    code: "",
    title: "",
    note: "",
    auditContent: "",
    auditUser: "",
    auditDate: ""
  }
});

// 接收父组件传过来的参数
const acceptParams = (params: DialogParam): void => {
  dialogData.optType = params.optType;
  dialogData.rowData = params.rowData;
  dialogData.isView = dialogData.optType === OptEnum.VIEW;
  dialogData.isAudit = dialogData.optType === OptEnum.AUDIT;
  dialogData.title = getOptTitle(dialogData.optType);

  if (dialogData.optType === OptEnum.CREATE) {
    initItemInfo();
  } else if (dialogData.rowData && dialogData.rowData.id) {
    getItemInfo(dialogData.rowData.id);
  }

  dialogVisible.value = true;
};

//初始化页面的数据
const initItemInfo = (): void => {
  dialogData.itemInfo = {
    code: "",
    title: "",
    note: "",
    auditContent: "",
    auditUser: "",
    auditDate: ""
  };
};

// //审核通过
// const handleAuditP = async () => {
//   console.log("Pass");
// };
// //审核不通过
// const handleAuditF = async () => {
//   console.log("failed");
// };

//数据较验
const rules = reactive({
  title: [{ required: true, message: "请填写标题", trigger: "blur" }]
});

//获取当前数据的详情，直接调用接口获取数据
const getItemInfo = async (id: string) => {
  if (id) {
    let { data } = await getIntegralAddInfoApi({ id });
    dialogData.itemInfo = data as unknown as IntegralAddEntity;
  }
};

// 提交数据（新增/编辑）
const integralAddFormRef = ref<FormInstance>();
const handleSubmit = () => {
  btnLoading.value = true;
  integralAddFormRef.value!.validate(async valid => {
    if (!valid) {
      btnLoading.value = false;
      return;
    }
    try {
      if (dialogData.optType === OptEnum.CREATE || dialogData.optType === OptEnum.EDIT) {
        //提交数据
        let res = await saveIntegralAddApi({
          ...dialogData.itemInfo
        });
        if (res.data) {
          ElMessage.success({
            message: `${dialogData.title}积分申请添加成功！`
          });
          //刷新列表数据
          emits("refresh-list");
          dialogVisible.value = false;
        }
      }
    } catch (error) {
    } finally {
      btnLoading.value = false;
    }
  });
};
//页面事件
const emits = defineEmits(["refresh-list"]);

//页面参数
defineExpose({
  acceptParams
});
</script>
